package com.woniuxy.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.woniuxy.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 这个用来学习控制器方法入参，通过controller的方法来接收前端的参数
 */
@Controller
@RequestMapping("/ruser")
public class RbacUserController {
    @RequestMapping("/add")
    public ModelAndView addUser(@RequestParam("name") String username, @RequestParam String password,Integer verificationCode){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username", "传入的名字是："+username);
        modelAndView.addObject("password", "传入的密码是："+password);
        modelAndView.addObject("verificationCode","传入的验证码是："+verificationCode);
        modelAndView.setViewName("stringview");
        return modelAndView;
    }

    /**
     *  数组类型参数接收
     * @param ids
     * @return
     */
    @RequestMapping("/deleteUsers")
    public ModelAndView deleteUsers(int[] ids){
        ModelAndView modelAndView = new ModelAndView();
        List<Integer> list = new ArrayList<>();
        for (int id : ids) {
            list.add(id);
        }
        modelAndView.addObject("ids", "传入的数组是："+list);
        modelAndView.setViewName("arrayview");
        return modelAndView;
    }

    /**
     *  List类型参数接收
     * @param ids
     * @return
     */
    @RequestMapping("/deleteChoices")
    public ModelAndView deleteChoices(@RequestParam("ids") List<Integer> ids){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("ids", "传入的数组是："+ids);
        modelAndView.setViewName("collectionview");
        return modelAndView;
    }

    /**
     * Map类型参数接收
     * @param user
     * @param ids
     * @return
     */
    @RequestMapping("/addUser")
    public ModelAndView addUser(@RequestParam Map user,@RequestParam List ids){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", "传入的Map集合是："+user);
        modelAndView.addObject("ids", "传入的List集合是："+ids);
        modelAndView.setViewName("mapview");
        return modelAndView;
    }

    /**
     *  用于处理 对象参数接收
     * @param student 对象类型接收 前面不用添加@ReqeustParam 否则返回会报400
     * @return
     */
    @RequestMapping("/updateUser")
    public ModelAndView updateUser(Student student){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("student", "传入的对象是："+student);
        modelAndView.setViewName("objectview");
        return modelAndView;
    }
}
